package rpc

import (
	. "leaf/core/error"
	. "leaf/core/msg"
	"reflect"
)

type RpcMethod func(agent interface{}, msg *PMsg) *Error

type RpcHandler struct {
	RequestId   uint16
	ReplyId     uint16
	RequestType reflect.Type
	ReplyType   reflect.Type
	Func        RpcMethod
	ServiceType reflect.Type
}

func (handler *RpcHandler) GetRequestMsg() interface{} {
	var val reflect.Value
	if handler.RequestType == nil {
		return nil
	}
	if handler.RequestType.Kind() == reflect.Ptr {
		val = reflect.New(handler.RequestType.Elem())
	} else {
		val = reflect.New(handler.RequestType)
	}
	return val.Interface()
}

func (handler *RpcHandler) GetReplyMsg() interface{} {
	var val reflect.Value
	if handler.ReplyType == nil {
		return nil
	}
	if handler.ReplyType.Kind() == reflect.Ptr {
		val = reflect.New(handler.ReplyType.Elem())
	} else {
		val = reflect.New(handler.ReplyType)
	}
	return val.Interface()
}
